//index.js
//获取应用实例
import WxValidate from '../../../utils/WxValidate.js'
const app = getApp()
Page({
  data: {
    navbarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '意见反馈', //导航栏 中间的标题
    },
    navHeight: app.globalData.statusBarHeight * 2 + 86,
    userMobile: app.globalData.userMobile,
    disabled: false//设置禁用
  },
  initValidate() {
    const rules = {

      mobile: {
        required: true,
        tel: true,
      },
      suggestion: {
        required: true
      },

    }
    // 验证字段的提示信息，若不传则调用默认的信息
    const messages = {
      mobile: {
        required: '请输入11位手机号码',
        tel: '请输入正确的手机号码',
      },
      suggestion: {
        required: '请输入您的意见或问题'
      },
    }
    // 创建实例对象
    this.WxValidate = new WxValidate(rules, messages)
  },
  onLoad: function () {
    this.initValidate();
    this.setData({
      userMobile: app.globalData.userMobile
    })
  },
  saveSuggestion: function (e) {
    //手机号验证码验证
    const params = e.detail.value
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      wx.showToast({
        title: error.msg,
        icon: 'none',
        duration: 1000
      })
      // this.showWarnInfo(error)
    } else {
      //
      this.setData({
        disabled: true//button解禁
      })
      wx.showLoading({
        title: '加载中',
      })
       //出现加载发生在请求之前
      wx.request({
        url: app.getUrl('/xcx/suggestion'),
        method: 'GET',
        header: wx.getStorageSync('cookie'),
        data: {
          mobile: e.detail.value.mobile,
          suggestion: e.detail.value.suggestion
        },
        //请求成功返回如下的操作
        success(res) {
          console.log(res);
          if (res.data.code == 0) {
            wx.hideLoading();//去掉加载页面
            wx.showModal({
              title: '成功',
              content: '意见提交成功',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  wx.navigateBack({
                    delta: 1
                  })
                }
              }
            })
          }
        }

      })
    }
  }
})